package com.sxj.web.jsp;

import com.sxj.web.util.CookieUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 石小俊
 * @date 2024年03月08日 20:19
 */
@WebServlet("*.cookie")
public class CookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = request.getServletPath();
        if("/add.cookie".equals(path)){
            addCookie(request,response);
        }
        if("/modify.cookie".equals(path)){
            modifyCookie(request,response);
        }
        if("/delete.cookie".equals(path)){
            deleteCookie(request,response);
        }
        if("/find.cookie".equals(path)){
            findCookie(request,response);
        }
    }

    public void addCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Cookie(String name, String value)
        // Cookie的key与value均为字符串
        // 说明Cookie只能保存字符串
//        Cookie cookie = new Cookie("username","admin");
//        // 默认情况下,当会话结束(关闭浏览器之后),Cookie自动删除
//        // 这是因此,Cookie的生存时间在于会话期间
//        // 我们可以手动指定生存时间
//        // 设置当前Cookie生存时间为30秒
//        cookie.setMaxAge(3);
//        Cookie cookie2 = new Cookie("password","123456");
////        Cookie cookie3 = new Cookie("name","管理员");
//        String name = "管理员";
//        name = URLEncoder.encode(name,"UTF-8");
//        Cookie cookie3 = new Cookie("name",name);
//        response.addCookie(cookie);
//        response.addCookie(cookie2);
//        response.addCookie(cookie3);

        CookieUtil.addCookie("username","admin",response);
        CookieUtil.addCookie("password","123456",response);
        CookieUtil.addCookie("name","管理员",response);
    }

    public void modifyCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Cookie的API中没有修改功能
        // 但是在添加Cookie的时候如果遇到同key的Cookie
        // 会自动覆盖原来的
        // 因此,修改本质上就是一个添加操作
//        Cookie cookie = new Cookie("password","111111");
//        response.addCookie(cookie);
        CookieUtil.modifyCookie("password","111111",response);
    }

    public void deleteCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 在Cookie的API中没有删除方法
        // 由于可以设置Cookie的生存时间
        // 当生存时间为0后,Cookie自动删除
        // 那么想要删除,只需要直接将其生存时间设置为0即可
        // 因此,删除的本质还是添加
//        Cookie cookie = new Cookie("password","");
//        cookie.setMaxAge(0);
//        response.addCookie(cookie);

        CookieUtil.deleteCookie("username",response);
    }

    public void findCookie(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Cookie[] cookies = request.getCookies();
//        for(Cookie cookie : cookies){
//            // 由于Cookie中保存的数据可能存在中文
//            // 因此,此处也需要进行转换
//            // 将Cookie中的数据转换成你所需要的字符集
//            System.out.println(cookie.getName()+":"+ URLDecoder.decode(cookie.getValue(),"UTF-8"));
//        }

        String name = CookieUtil.findByName("name",request);
        System.out.println("name:"+name);
    }
}
